package com.example.design.c.state.demo3;

import cn.hutool.core.date.DateUtil;

/**
 * 就绪状态
 */
public class StateRunnable extends ThreadAbstractState {
    /**
     * 构造方法初始化线程状态名称
     */
    public StateRunnable() {
        this.setStateName("就绪");
    }

    @Override
    public boolean getCPU(ThreadContext context) {
        System.out.println("============================");
        System.out.println("获取CPU时间: " + DateUtil.now());

        String stateName = context.getState().getStateName();

        if ("就绪".equals(stateName)) {
            context.setState(new StateRunning());
            context.showState();
            return true;
        }
        System.out.println("非'就绪'状态无法调用getCPU方法");
        return false;
    }
}
